package com.Txunda.fxw.base.utils.tool;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.ClipboardManager;
import android.util.Log;

/**
 * 工具类
 * 
 * @author Zero @date 2014年8月10日
 * @version 1.0
 */
@SuppressWarnings("deprecation")
public class Toolkit {

	/**
	 * 得到手机IP地址
	 * 
	 * @return
	 */
	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = (NetworkInterface) en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @Title: getLocalIpAddress2
	 * @Description(描述): 获取移动网络的IP,格式为0.0.0.0
	 * @param(参数): 
	 *			@return
	 * @return(返回值)： String
	 * @throws
	 */
	public static String getLocalIpAddress2() {
		String ip =null;
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = (NetworkInterface) en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						ip = intToIp(inetAddress.getHostAddress().hashCode());
					}
				}
			}
			return ip;
		} catch (Exception e) {
			e.printStackTrace();
			return ip;
		}
	}

	/**
	 * 
	 * @Title: getWifiIP
	 * @Description(描述): 获取wifi的IP地址，格式为 	221.238.193.163
	 * @param(参数): 
	 *			@param context
	 *			@return
	 * @return(返回值)： String
	 * @throws
	 */
	public static String getWifiIP(Context context) {
		try {
			// 获取wifi服务
			WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			// 判断wifi是否开启
			if (!wifiManager.isWifiEnabled()) {
				wifiManager.setWifiEnabled(true);
			}
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			int ipAddress = wifiInfo.getIpAddress();
			return intToIp(ipAddress);
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 
	 * @Title: intToIp
	 * @Description(描述): 将一个int类型的数字转换成0.0.0.0格式的IP地址
	 * @param(参数): 
	 *			@param i
	 *			@return
	 * @return(返回值)： String
	 * @throws
	 */
	private static String intToIp(int i) {

		/**
		 * 负数&步骤：
		 * 1.将负数的绝对值转成二进制
		 * 2.获得二进制的反码（将二进制---0变1，1变0）
		 * 3.获得补码（反码+1）
		 * 4.与运算
		 * 
		 * 负数位移步骤：获得补码后位移
		 */
		return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF);
	}

	/**
	 * 
	 * @Title: ipForNetType
	 * @Description(描述): 根据网络状态获取IP地址，格式为0.0.0.0
	 * @param(参数): 
	 *			@param content
	 *			@return
	 * @return(返回值)： String
	 * @throws
	 */
	public static String ipForNetType(Context content) {
		// TODO Auto-generated method stub
		String type = Toolkit.getCurrentNetType(content);
		if (type != null) {
			if ("wifi".equals(type)) {
				return getWifiIP(content);
			} else {
				return getLocalIpAddress2();
			}
		}
		return null;
	}

	/**
	 * 实现文本复制功能
	 * 
	 * @param content
	 */
	public static void copy(Context context, String content) {
		// 得到剪贴板管理器
		ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(content.trim());
	}

	/**
	 * 实现粘贴功能
	 * 
	 * @param context
	 * @return
	 */
	public static String paste(Context context) {
		// 得到剪贴板管理器
		ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		return cmb.getText().toString().trim();
	}

	/**
	 * 判断字符串内容是否为数字
	 * 
	 * @param str
	 * @return boolean
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * 判断邮箱格式是否正确
	 */
	public static boolean isEmail(String strEmail) {
		String strPattern = "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$";

		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(strEmail);
		return m.matches();
	}

	/**
	 * 判断手机号码
	 */
	public static boolean isMobile(String mobiles) {
		//								   ^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$
		Pattern pattern = Pattern.compile("^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}|17[0-9]{9}$");
		Matcher matcher = pattern.matcher(mobiles);
		return matcher.matches();
	}

	/**
	 * 获取屏幕的宽
	 * 
	 * @param context
	 * @return
	 *         <p>
	 *         int
	 */
	public static int getScreenWidth(Context context) {
		return context.getResources().getDisplayMetrics().widthPixels;
	}

	/**
	 * 获取屏幕的高
	 * 
	 * @param context
	 * @return
	 *         <p>
	 *         int
	 */
	public static int getScreenHeight(Context context) {
		return context.getResources().getDisplayMetrics().heightPixels;
	}

	/**
	 * 获取手机状态栏高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getStatusBarHeight(Context context) {
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0;
		int statusBarHeight = 0;
		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			statusBarHeight = context.getResources().getDimensionPixelSize(x);
			return statusBarHeight;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statusBarHeight;
	}

	/**
	 * 获取config.properties文件中的值 assets
	 * 
	 * @param key
	 * @return String
	 */
	public static String getConfigProperties(String key) {
		String value = "";
		Properties props = new Properties();
		InputStream iStream = Toolkit.class.getResourceAsStream("/assets/config.properties");
		try {
			props.load(iStream);
			value = props.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return value;
	}

	/**
	 * <p>
	 * 获取图片资源的id
	 * 
	 * @param context
	 * @param resName
	 *            图片资源的名称
	 * @return int 图片资源id
	 */
	public static int getBitmapRes(Context context, String resName) {
		try {
			String str = context.getPackageName();
			Class localClass = Class.forName(str + ".R$drawable");
			return getResId(localClass, resName);
		} catch (Throwable localThrowable) {
			// CommonlyLog.c(localThrowable);
		}
		return 0;
	}

	/**
	 * <p>
	 * 获取控件资源的id
	 * 
	 * @param context
	 * @param resName
	 *            图片资源的名称
	 * @return int 图片资源id
	 */
	public static int getViewRes(Context context, String resName) {
		try {
			String str = context.getPackageName();
			Class localClass = Class.forName(str + ".R$id");
			return getResId(localClass, resName);
		} catch (Throwable localThrowable) {
			// CommonlyLog.c(localThrowable);
		}
		return 0;
	}

	/**
	 * <p>
	 * 获取文字资源的id
	 * 
	 * @param context
	 * @param resName
	 *            文字资源的名称
	 * @return int 文字资源的id
	 */
	public static int getStringRes(Context context, String resName) {
		try {
			String str = context.getPackageName();
			Class localClass = Class.forName(str + ".R$string");
			return getResId(localClass, resName);
		} catch (Throwable localThrowable) {
			// CommonlyLog.c(localThrowable);
		}
		return 0;
	}

	private static int getResId(Class<?> paramClass, String paramString) {
		int i = 0;
		if (paramString != null) {
			String str = paramString.toLowerCase();
			try {
				Field localField = paramClass.getField(str);
				localField.setAccessible(true);
				i = ((Integer) localField.get(null)).intValue();
			} catch (Throwable localThrowable) {
				// CommonlyLog.c(localThrowable);
				i = 0;
			}
		}
	 
		return i;
	}

	/** 把list转成json字符串 */
	public static String getJson(List<Map<String, String>> list) {
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for (int i = 0; i < list.size(); i++) {
			Map<String, String> object = list.get(i);
			sb.append("{");
			Iterator<String> iterator = object.keySet().iterator();
			while (iterator.hasNext()) {
				String key = iterator.next();
				sb.append("\"" + key + "\":\"" + object.get(key) + "\"");
				if (iterator.hasNext())
					sb.append(",");
			}
			sb.append("}");
			if (i != list.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		return sb.toString();
	}

	/**
	 * 获取网络类型
	 * 
	 * @param context
	 * @return
	 */
	public static String getCurrentNetType(Context context) {
		String network_type = "";
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info == null) {
			network_type = "null";
		} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
			network_type = "wifi";
		} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
			int subType = info.getSubtype();
			if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
					|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
				network_type = "2g";
			} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_0
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
				network_type = "3g";
			} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡，是3.9G的全球标准
				network_type = "4g";
			}
		}
		return network_type;
	}

	/**
	 * 获取运营商名字
	 * 
	 * @param context
	 * @return
	 */
	public String getOperatorName(Context context) {
		TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String operator = telManager.getSimOperator();
		String operator_name = null;
		if (operator != null) {
			if (operator.equals("46000") || operator.equals("46002") || operator.equals("46007")) {
				// 中国移动
				operator_name = "中国移动";
				return operator_name;
			} else if (operator.equals("46001")) {
				// 中国联通
				operator_name = "中国联通";
				return operator_name;
			} else if (operator.equals("46003")) {
				// 中国电信
				operator_name = "中国电信";
				return operator_name;
			}
		}
		return null;
	}

	/**
	 * 判断是否可解析成集合类型
	 * 
	 * @param map
	 *            解析出的map集合
	 * @param type
	 *            map 的 key，例如：解析出的map为data，则type就是将data里的数据解析成list集合的key
	 * @return
	 */
	public static boolean IsList(Map<String, String> map, String type) {
		if (!map.get(type).equals("") && !map.get(type).equals("null") && !map.get(type).equals("[]")
				&& map.get(type) != null && !map.get(type).equals("[null]")) {
			return true;
		}
		return false;

	}

	/**
	 * 判断底部是否存在NavigationBar
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkDeviceHasNavigationBar(Context context) {
		boolean hasNavigationBar = false;
		Resources rs = context.getResources();
		int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
		if (id > 0) {
			hasNavigationBar = rs.getBoolean(id);
		}
		try {
			Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
			Method m = systemPropertiesClass.getMethod("get", String.class);
			String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
			if ("1".equals(navBarOverride)) {
				hasNavigationBar = false;
			} else if ("0".equals(navBarOverride)) {
				hasNavigationBar = true;
			}
		} catch (Exception e) {
			Log.w("一场", e);
		}

		return hasNavigationBar;

	}

	/**
	 * 获取NavigationBar的高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getNavigationBarHeight(Context context) {
		int navigationBarHeight = 0;
		Resources rs = context.getResources();
		int id = rs.getIdentifier("navigation_bar_height", "dimen", "android");
		if (id > 0 && checkDeviceHasNavigationBar(context)) {
			navigationBarHeight = rs.getDimensionPixelSize(id);
		}
		return navigationBarHeight;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 将px值转换为sp值，保证文字大小不变
	 * 
	 * @param pxValue
	 * @param fontScale
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static int px2sp(Context context, float pxValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;// 屏幕密度
		return (int) (pxValue / fontScale + 0.5f);
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 * 
	 * @param spValue
	 * @param fontScale
	 *            （DisplayMetrics类中属性scaledDensity）
	 * @return
	 */
	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}

	/**
	 * 
	 * @Title: getApplicationName
	 * @Description(描述): 获取应用名称
	 * @param(参数): 
	 *			@param context
	 *			@return
	 * @return(返回值)： String
	 * @throws
	 */
	public static String getApplicationName(Context context) {
		PackageManager packageManager = null;
		ApplicationInfo applicationInfo = null;
		try {
			packageManager = context.getApplicationContext().getPackageManager();
			applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
		} catch (PackageManager.NameNotFoundException e) {
			applicationInfo = null;
		}
		String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
		return applicationName;
	}

	/**
	 * 
	 * @Title: createFile
	 * @Description(描述): 日志输出文件------>>>>SD卡根目录
	 * @param(参数): 
	 *			@return
	 * @return(返回值)： File
	 * @throws
	 */
	public static File createFile(Context context) {
		// TODO Auto-generated method stub

		/**
		 * Environment.getExternalStorageDirectory()  获取的是SD卡的根目录
		 * File.separator   为分隔符
		 */
		File logf = new File(
				Environment.getExternalStorageDirectory() + File.separator + getApplicationName(context) + ".txt");
		return logf;

	}

}
